package org.despair.net.socket 
{
	import org.despair.net.socket.parse.IParseSocketData;
	import flash.events.ErrorEvent;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.Socket;
	import flash.utils.ByteArray;
	

	
	
	
public class ZSocket
{
	public function ZSocket(endian:String, handler:ISocketHandler , parser:IParseSocketData) 
	{
		m_endian = endian;
		m_handler = handler;
		m_parser = parser;
	}
	
	
	
	
	public function get isConnected():Boolean
	{	
		return m_socket && m_socket.connected; 
	}
	
	
	/**
	 * 连接
	 */
	public function connect(ip:String , port:int):void
	{
		if (m_socket && m_socket.connected)
		{
			onSocketClose(Event(""));
		}
			
		m_parser.clearBuffer();
		m_socket = new Socket();
		m_socket.endian = m_endian;
		m_socket.addEventListener(Event.CONNECT , onConnect);
		m_socket.addEventListener(IOErrorEvent.IO_ERROR , onIOError);
		m_socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR , onSecurityError);
		m_socket.connect(ip , port);
	}
	
	
	/**
	 * 关闭
	 */
	public function close():void
	{
		removeEvent();
		if (m_socket)
		{
			m_socket.close();
			m_socket = null;
		}
	}
	
	/**
	 * 发送数据
	 */
	public function sendData(bytes:ByteArray):void
	{
		if ( _socket == null || !_socket.connected)
			return;
		_socket.writeBytes(bytes);
		_socket.flush();
	}
	
	
	/// 添加事件
	private function addEvent():void
	{
		_socket.addEventListener(Event.CLOSE , onSocketClose);
		_socket.addEventListener(Event.CONNECT , onConnect);
		_socket.addEventListener(ProgressEvent.SOCKET_DATA , onSocketData);
		_socket.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
	}
	
	/// 移除事件
	private function removeEvent():void
	{
		if(_socket){
			if(_socket.hasEventListener(IOErrorEvent.IO_ERROR)) _socket.removeEventListener(IOErrorEvent.IO_ERROR, onIOError);
			if(_socket.hasEventListener(Event.CONNECT)) _socket.removeEventListener(Event.CONNECT, onConnect);
			if(_socket.hasEventListener(ProgressEvent.SOCKET_DATA)) _socket.removeEventListener(ProgressEvent.SOCKET_DATA,onSocketData);
			if(_socket.hasEventListener(Event.CLOSE)) _socket.removeEventListener(Event.CLOSE,onSocketClose);
			if(_socket.hasEventListener(SecurityErrorEvent.SECURITY_ERROR)) _socket.removeEventListener(SecurityErrorEvent.SECURITY_ERROR,onSecurityError);
		}
	}
	
	/// 服务器关闭
	private function onSocketClose(e:Event):void 
	{
		close();
		_handler.onClose();
	}
	
	/// 连接
	private function onConnect(e:Event):void 
	{
		removeEvent();
		m_socket.addEventListener(ProgressEvent.SOCKET_DATA , onSocketData);
		m_socket.addEventListener(Event.CLOSE , onSocketClose);
		m_handler.onConnect();
	}
	
	/// IO 错误
	private function onIOError(e:IOErrorEvent):void 
	{
		onError(e);
	}
	
	/// 安全域错误
	private function onSecurityError(e:SecurityErrorEvent):void 
	{
		onError(e);
	}
	
	/// 收到数据
	private function onSocketData(e:ProgressEvent):void 
	{
		//trace("长度：" + e.currentTarget.bytesAvailable);
		m_parser.parse(_socket);
	}
	
	private function onError(e:ErrorEvent):void
	{
		close();
		m_handler.onError(e);
	}
	
	
	
	
	//======================
	// Member
	//======================
	
	
	private var m_socket:Socket;	
	
	private var m_handler:ISocketHandler;
	
	private var m_parser:IParseSocketData;
	
	private var m_endian:String;
		
}

}